package com.imooc.portal.mq;

import com.imooc.business.processor.UpDimensionRequestProcessor;
import com.imooc.module.collect.request.ParamsRequest;
import com.imooc.portal.mq.entity.EventProcessMessage;
import lombok.Setter;
import org.apache.rocketmq.spring.annotation.RocketMQMessageListener;
import org.apache.rocketmq.spring.core.RocketMQListener;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

/**
 * @Author Tim
 * @Description:
 * @Version 1.0
 */
@Component
@RocketMQMessageListener(topic = "${topic.data-center.updimension}", consumerGroup = "rocketMqUpDimensionConsumer-group")
public class RocketMqUpDimensionConsumer implements RocketMQListener<EventProcessMessage> {

    @Setter(onMethod_ = @Autowired)
    private UpDimensionRequestProcessor upDimensionRequestProcessor;

    //redis
    @Override
    public void onMessage(EventProcessMessage message) {
        //做重复消费
        String uuid = message.getUuid();
        ParamsRequest request = buildRequest(message);
        upDimensionRequestProcessor.callback(request);
    }

    /**
     * 构建维度升级请求
     * @param message
     * @return
     */
    private ParamsRequest buildRequest(EventProcessMessage message) {
        ParamsRequest request = new ParamsRequest();
        request.setBusinessId(message.getBusinessId());

        ParamsRequest.DimensionParameter dimension = message.getDimension();
        ParamsRequest.DimensionParameter dimensionParameter = new ParamsRequest.DimensionParameter();
        dimensionParameter.setDimension(dimension.getDimension());
        dimensionParameter.setUserIds(dimension.getUserIds());
        dimensionParameter.setGroupId(dimension.getGroupId());
        dimensionParameter.setSchoolId(dimension.getSchoolId());
        request.setDimensionParameter(dimensionParameter);

        request.setPeriodType(message.getPeriodType());
        request.setRequestDate(message.getRequestDate());
        request.setStartTime(message.getStartTime());
        request.setEndTime(message.getEndTime());
        return request;
    }
}
